package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.mapper.UserProductPreferenceMapper;
import com.qys.livesMall.shop.modal.dto.QueryProductsDTO;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.ShopGoodsInfo;
import com.qys.livesMall.shop.mapper.ShopGoodsInfoMapper;
import com.qys.livesMall.shop.modal.entity.UserProductPreference;
import com.qys.livesMall.shop.service.ShopGoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gmx
 * @since 2023-06-07
 */
@Service
public class ShopGoodsInfoServiceImpl extends ServiceImpl<ShopGoodsInfoMapper, ShopGoodsInfo> implements ShopGoodsInfoService {

    @Resource
    private UserProductPreferenceMapper userProductPreferenceMapper;

    @Autowired
    private ShopGoodsInfoMapper shopGoodsInfoMapper;

    @Autowired
    private H2ShopBaseDao shopBaseDao;

    @Override
    public List<ShopGoodsInfo> getGoodsInfo(String goodsId, String shopId) {
        LambdaQueryWrapper<ShopGoodsInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoodsInfo::getGoodsId,goodsId);
        queryWrapper.eq(ShopGoodsInfo::getShopId,shopId);
        List<ShopGoodsInfo> infoList = super.list(queryWrapper);
        return infoList;
    }

    @Override
    public List<ShopGoodsInfo> productRecommended() {
        UserProductPreference userProductPreference = userProductPreferenceMapper.selectOne(Wrappers.<UserProductPreference>lambdaQuery()
                .eq(UserProductPreference::getUserId, SecurityUtils.getUserId())
                .orderByDesc(UserProductPreference::getViews)
                .last("limit 1"));
        return baseMapper.productRecommended(userProductPreference == null ? null : userProductPreference.getProductCategoryId());
    }

    @Override
    public List<ShopGoodsInfo> queryProductsBasedOnUserPreferences(QueryProductsDTO dto) {
        UserProductPreference userProductPreference=null;
        if (SecurityUtils.getUserId()!=null) {
            userProductPreference = userProductPreferenceMapper.selectOne(Wrappers.<UserProductPreference>lambdaQuery()
                    .eq(UserProductPreference::getUserId, SecurityUtils.getUserId())
                    .orderByDesc(UserProductPreference::getViews)
                    .last("limit 1"));
        }

        QueryWrapper<ShopGoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", dto.getShopId());
        wrapper.eq("status", 1);
        if(userProductPreference != null || dto.getProductCategoryId() != null){
            wrapper.eq("category_two_id", dto.getProductCategoryId() == null ? userProductPreference.getProductCategoryId() : dto.getProductCategoryId());
        }
        if (dto.getSortType() != null) {
            switch (dto.getSortType()) {
                case 2 -> wrapper.orderByDesc("sales_volume");
                case 3 -> wrapper.orderByDesc("create_time");
                case 4 -> wrapper.orderByDesc("price");
                case 5 -> wrapper.orderByAsc("sales_volume");
                case 6 -> wrapper.orderByAsc("create_time");
                case 7 -> wrapper.orderByAsc("price");
                default -> wrapper.orderByAsc("rand()");
            }
        }
        wrapper.last("limit 10");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public LocalDateTime getLastUpShelfTime(Long shopId) {
        return shopGoodsInfoMapper.getLastUpShelfTime(shopId);
    }

    @Override
    public void upDown(Long shopGoodsId, boolean status) {
        ShopGoodsInfo goodsInfo=getById(shopGoodsId);
        goodsInfo.setStatus(status);
        updateById(goodsInfo);
        LambdaQueryWrapper<ShopGoodsInfo> shopGoodsInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::getShopId,goodsInfo.getShopId());
        shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::isStatus,true);
        shopGoodsInfoLambdaQueryWrapper.eq(ShopGoodsInfo::getIsDelete,"1");
        Long shopProductNum =count(shopGoodsInfoLambdaQueryWrapper);
        H2ShopBase shopBase = shopBaseDao.getById(goodsInfo.getShopId());
        shopBase.setShopProductNum(Integer.valueOf(shopProductNum.toString()));
        shopBaseDao.updateById(shopBase);
    }
}
